package com.south.base.test.proxy;

import com.south.base.proxy.ProxyInterface;
import com.south.base.proxy.RealObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

/**
 * @author Administrator
 * @date 2019/02/27 10:31
 * @description
 */
@Slf4j
public class DynamicProxy {
    @Test
    public void test() {
        RealObject real = new RealObject();
        consumer(real);

        log.warn("jdk proxy");
        ProxyInterface proxy = (ProxyInterface) Proxy.newProxyInstance(
                ProxyInterface.class.getClassLoader(),
                new Class[]{ProxyInterface.class},
                new com.south.base.proxy.jdk.DynamicProxyHandler(real));

        consumer(proxy);

        log.warn("cglib proxy");
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(real.getClass());
        enhancer.setCallback(new com.south.base.proxy.cglib.DynamicProxyHandler(real));
        consumer((ProxyInterface) enhancer.create());

    }

    private void consumer(ProxyInterface proxyInterface) {
        proxyInterface.doSomething();
        proxyInterface.somethingElse("hello world");
    }
}